import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Jiange
 * @create 2021-07-16-9:30
 */
public class ReentrantLockTest {
    public static void main(String[] args) {
        Toicket ticket = new Toicket();

        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"AA").start();
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"BB").start();
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"CC").start();
    }
}
class Toicket{
    private int number = 30;
    private final ReentrantLock lock = new ReentrantLock();

    public void sale(){
        lock.lock();
        try{
            if(number>0){
                System.out.println(Thread.currentThread().getName()+"---------"+number);
                number--;
            }
        }finally {
            lock.unlock();
        }
    }
}
